<?php
class UserController extends Controller
{
	public function init()
	{
		parent::init();
		if (Yii::app()->user->isGuest){
			$this->redirect(Yii::app()->createUrl('auth/login', array('url'=>urlencode(Utils::getCurrentUrl()))));
		}
	}
	public function actionIndex()
	{
		if(Yii::app()->request->isPostRequest)
		{
			$args = Yii::app()->request->getParam('user', null);
			try{
				$args['user_username'] = $args['user_email'];
				
				//check if input is valid
				if(empty($args['user_email']) || !filter_var($args['user_email'], FILTER_VALIDATE_EMAIL)){
					throw new Exception(Yii::t('helpdesk','Invalid data'));
				}
				
				//update user info
				$intResult = Users::getInstance()->update(Yii::app()->user->id, $args);
				
				//upload avatar
				if(!empty($_FILES['avatar'])){
					if($_FILES['avatar']['error'] != UPLOAD_ERR_NO_FILE){
						$objFileTransfer = new FileTransfer(UPLOADS_DIR, array('prefixPath'=>'avatar/'.date('Y').'/'.date('md'), 'allowedFileExtension'=>'jpg,jpeg,png,gif'));
						$arrFileInfo = $objFileTransfer->upload($_FILES['avatar']);
						if($arrFileInfo !== false){
							$result = Media::getInstance()->insert(array(
								'media_title' => $arrFileInfo['filename'],
								'media_file' => $arrFileInfo['path'],
								'media_file_size' => $arrFileInfo['size'],
								'media_file_extension' => $arrFileInfo['extension'],
								'media_src' => 1,
								'media_folder' => 'avatar_'.Yii::app()->user->id,
								'media_creator_id' => Yii::app()->user->id
							));
							if($result){
								//$this->thumb($arrFileInfo);
							}
						}
					}
				}
				
				//update profile
				$hasProfile = true;
				$profile = Users::getInstance()->getProfile(Yii::app()->user->id);
				if(empty($profile)){
					$hasProfile = false;
				}
				$profile['profile_email'] = $args['user_username'];
				$profile['profile_phone'] = trim(Yii::app()->request->getPost('phone', null));
				$profile['profile_address'] = trim(Yii::app()->request->getPost('address', null));
				if(isset($arrFileInfo['path'])){
					$profile['profile_image'] = $arrFileInfo['path'];
				}
				$profile['profile_allow_notify'] = trim(Yii::app()->request->getParam('notification', null));
				$profile['profile_status'] = 1;
				if(!$hasProfile){
					Users::getInstance()->insertProfile(Yii::app()->user->id, $profile);
				} else {
					Users::getInstance()->updateProfile(Yii::app()->user->id, $profile);
				}
				
				//update password
				$password = Yii::app()->request->getParam('user_password', null);
				if($password != ''){
					Users::getInstance()->updatePassword(Yii::app()->user->id, $password);
				}
				
				//write log 
				Logs::getInstance()->insert(array(
					'user_id' => Yii::app()->user->id,
					'user_ip' => Utils::getIp(),
					'user_agent' => $_SERVER['HTTP_USER_AGENT'],
					'log_action' => 'HELPDESK_UPDATE_PROFILE',
					'log_message' => '<b>'.Yii::app()->user->getState('fullname').'</b> '.Yii::t('helpdesk','updated profile')
				));
							
				//done
				Yii::app()->user->setFlash('ok', Yii::t('helpdesk', 'Updated successfully!').' '.Yii::t('helpdesk', 'Please re-login to apply changes'));
				$this->refresh();
			} catch(Exception $ex){
				Yii::app()->user->setFlash('error', $ex->getMessage());
			}
		}
		//get user & profile info
		$user = Users::getInstance()->get(Yii::app()->user->id);
		$profile = Users::getInstance()->getProfile(Yii::app()->user->id);
		$this->render('index', array(
			'user' => $user,
			'profile' => $profile
		));
	}
	
}